﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.ViewModels;
using Enterprise.Management.Business;
using Enterprise.Management.FilterAttributes;

namespace Enterprise.Management.Controllers
{
    [CategoryFunction]
    public class RewardsPunishmentController : BaseController
    {
        private RewardsPunishmentBLL bll = new RewardsPunishmentBLL();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(RewardsPunishmentViewModel model)
        {
            if (ModelState.IsValid)
            {
                DP_Reward_Info rp = new DP_Reward_Info();
                rp.RwdInf_Date = model.Date.Value;
                rp.RwdInf_DeptID = model.DeptId;
                rp.RwdInf_ResultID = model.ResultId;

                if (bll.Add(rp))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "新建失败，请稍后再试！");
                }
            }

            return View(model);
        }

        public ActionResult Info(int id)
        {
            var rp = bll.Get(id);
            return View(rp);
        }

        public ActionResult Update(int id)
        {
            var rp = bll.Get(id);
            RewardsPunishmentViewModel model = new RewardsPunishmentViewModel();
            model.Id = rp.RwdInf_ID;
            model.Date = rp.RwdInf_Date;
            model.DeptId = rp.RwdInf_DeptID;
            model.ResultId = rp.RwdInf_ResultID;
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(RewardsPunishmentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var rp = bll.Get(model.Id.Value);
                rp.RwdInf_Date = model.Date.Value;
                rp.RwdInf_DeptID = model.DeptId;
                rp.RwdInf_ResultID = model.ResultId;
                if (bll.Update(rp))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "修改失败，请稍后再试！");
                }
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            bll.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
